{extend name="common/base" /}

{block name="title"}<title>{$title}</title>{/block}

{block name="main"}
<div class="layui-fluid table">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="layui-form" lwid100="" iwid300="" lay-filter="form">

                        {include file="common/form" /}

                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit="" lay-filter="form">保存</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<input type="hidden" id="type" value="1">
<input type="hidden" id="time" value="1">
<input type="hidden" id="alert" value="1">
{:token_meta()}
{/block}

<script>
    {block name="layui_table"}
    laydate.render({
        elem: '#time_start'
        ,type: 'datetime'
        ,value: getRecentDay(0)
    });

    function getRecentDay(day){
        var today = new Date();
        var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
        today.setTime(targetday_milliseconds);
        var tYear = today.getFullYear();
        var tMonth = today.getMonth();
        var tDate = today.getDate();
        var tHours = today.getHours();
        var tMinutes = today.getMinutes();
        var tSeconds = today.getSeconds();
        tMonth = doHandleMonth(tMonth + 1);
        tDate = doHandleMonth(tDate);
        return tYear+"-"+tMonth+"-"+tDate+" "+tHours+":"+tMinutes+":"+tSeconds;
    }
    function doHandleMonth(month){
        var m = month;
        if(month.toString().length == 1){
            m = "0" + month;
        }
        return m;
    }

    var index = parent.layer.getFrameIndex(window.name);

    $('#close').on("click",function () {
        parent.layer.close(index);
    });


    // 获取默认选中的值
    var defaultVipClassValue = $('#vip_class_id').val();

    $("#type").val(defaultVipClassValue);

    $('#member_id,#mobile').on("blur",function () {
        $('#user').hide();
        data = {'sign_id':$('#sign_id').val(),'member_id':$('#member_id').val(),'mobile':$('#mobile').val()};
        res = curl('/backmanage/anchor/getUser',data,'POST');
        if (res.code == 0){
            user = res.data;
        } else{
            layer.msg(res.msg);
            if(this.id =='member_id'){
                col = 'mobile';
            }else{
                col = 'member_id';
            }
            $('#'+col).val('');
            $('#nick_name').html('');
            $('#balance').val('');
            return;
        }

        $('#mobile').val(user.mobile);
        $('#member_id').val(user.id);
        $('#nick_name').html(user.nickname);
        $('#balance').val(user.balance);
        $('#user').show();

        var now = new Date().getTime()/1000;
        if($('#type').val() == 1){
            end_time = Date.parse(user.vip_end_time)/1000+3600*24*30;
            if(now > user.vip_end_time) end_time = now+3600*24*30;
        }else if($('#type').val() == 7){
            end_time = Date.parse(user.vip_end_time)/1000+3600*24*90;
            if(now > user.vip_end_time) end_time = now+3600*24*90;
        }else{
            end_time = Date.parse(user.vip_end_time)/1000+3600*24*365;
            if(now > user.vip_end_time) end_time = now+3600*24*365;
        }
        time = new Date(parseInt(end_time) * 1000).toLocaleString().split(' ')[0];
        $('#vip_end_time').val(time);
    });

    form.on('select(select)', function(data){
        var now = new Date().getTime()/1000;
        $('#type').val(data.value);
        if($('#type').val() == 1){
            end_time = Date.parse(user.vip_end_time)/1000+3600*24*30;
            if(now > user.vip_end_time) end_time = now+3600*24*30;
        }else if($('#type').val() == 7){
            end_time = Date.parse(user.vip_end_time)/1000+3600*24*90;
            if(now > user.vip_end_time) end_time = now+3600*24*90;
        }else{
            end_time = Date.parse(user.vip_end_time)/1000+3600*24*365;
            if(now > user.vip_end_time) end_time = now+3600*24*365;
        }
        time = new Date(parseInt(end_time) * 1000).toLocaleString().split(' ')[0];
        $('#vip_end_time').val(time);
    });

    form.on('submit(form)', function(data){

        if($('#alert').val() ==1){
            $('#alert').val(2);

            $.ajax({
                url: '/backmanage/member_vip_order/create'
                ,data: data.field
                ,type: 'POST'
                ,headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
                ,success(res){
                    if(typeof  res == "string"){
                        res = JSON.parse(res);
                    }
                    if (res.code == 0){
                        parent.layer.msg(res.msg, {icon: 1});
                        parent.layer.close(index);
                        parent.layui.table.reload('table',{
                            where: {
                                filter:{'sign_id':data.field.sign_id}
                            }
                        });
                    } else if($('#alert').val() ==2){
                        $('#alert').val(1);
                        layer.msg(res.msg);
                    }
                }
            }).fail(function(msg){
                $('#alert').val(1);
                layer.open({
                    content: '操作失败,请稍候再试',
                    btn: ['OK']
                });
            });
        }
    });


    var list_url,cols ='';
    {/block}
</script>

